import java.util.PriorityQueue;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-07-11
 * Time: 11:52
 * To BE A Better Person-2020-3-2
 */

public class TestDemo {
    public static int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<Integer>((o1,o2)-> o2-o1);
        for (int i = 0; i < stones.length; i++) {
            priorityQueue.offer(stones[i]);
        }
       
        while(priorityQueue.size()>1){
            int a=priorityQueue.poll();
            int b=priorityQueue.poll();
            if(Math.abs(a-b)!=0) {
                priorityQueue.offer(Math.abs(a - b));
            }
        }

        if(priorityQueue.size()==0){
            return -1;
        }else{
            return priorityQueue.poll();
        }
    }

    public static void main(String[] args) {
        int[] stones={2,7,4,1,8,1};
        int ret=lastStoneWeight(stones);
        System.out.println(ret);
    }
}
